<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" v-model="inputContent"></a-input>
    <a-button type="primary" @click="addValueToList">添加事项</a-button>

    <a-list bordered :dataSource="showList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <a-checkbox
          :checked="item.done"
          @change="(e)=>{changeCheckboxStatus(e,item.id)}"
        >{{item.info}}</a-checkbox>
        <a slot="actions" @click="removeItemById(item.id)">删除</a>
      </a-list-item>

      <div class="footer" slot="footer">
        <span>{{remain}}条剩余</span>

        <a-button-group>
          <a-button :type="statusKey==='all'?'primary':'default'" @click="changeStatus('all')">全部</a-button>
          <a-button :type="statusKey==='nodo'?'primary':'default'" @click="changeStatus('nodo')">未完成</a-button>
          <a-button :type="statusKey==='done'?'primary':'default'" @click="changeStatus('done')">已完成</a-button>
        </a-button-group>

        <a slot="actions" @click="clearDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
/* eslint-disable */
import { mapState, mapActions, mapMutations, mapGetters } from 'vuex'
export default {
  name: 'App',
  data () {
    return {}
  },
  methods: {
    ...mapActions(['getListAsync']),
    ...mapMutations(['setInputValue', 'addValue', 'removeItem', 'changeCheckbox', 'clear']),
    //添加数据
    addValueToList () {
      if (this.inputContent.trim() === '') {
        return this.$message.warning('请输入内容')
      }
      this.addValue()
    },
    //删除数据
    removeItemById (id) {
      this.removeItem(id)
    },
    //改变复选框的状态
    changeCheckboxStatus (e, id) {
      const param = {
        status: e.target.checked,
        id
      }
      this.changeCheckbox(param)
    },
    //清除已经完成
    clearDone () {
      this.clear()
    },
    // 改变显示是否完成任务的状态
    changeStatus (status) {
      this.$store.commit('changeStatusKey', status)
    }
  },
  computed: {
    ...mapGetters(['remain','showList']),
    ...mapState(['list', 'inputValue','statusKey']),
    //输入表单和vuex里的数据绑定
    inputContent: {
      get () {
        return this.inputValue
      },
      set (inputContent) {
        this.setInputValue(inputContent)
      }
    }
  },
  mounted () {
    this.getListAsync()
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}
.my_ipt {
  width: 500px;
  margin-right: 10px;
}
.dt_list {
  width: 500px;
  margin-top: 10px;
}
.footer {
  display: flex;
  justify-content: space-around;
  align-items: center;
}
</style>
